﻿using MonitorPrism_Practice.Model;
using MonitorPrism_Practice.Mvvm;
using MonitorPrism_Practice.Services;
using MonitorPrism_Practice.Win32;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using static MonitorPrism_Practice.EnumData.KeyBoardMouseVlaueCollection;

namespace MonitorPrism_Practice.ViewModels.SettingConfigViewModel
{
    public class AddWriteDataViewModel : BindableBase, IViewLoadedAndUnloadedAware
    {
        Window window;
        private WriteDataEventService WriteDataEvent { get; }
        private bool _hasTriggered = false; // 记录是否已触发过
        private Timer inputTimer;
        private readonly int checkInterval = 50; // 检测间隔（毫秒）
        IkeyBoardMouseClickService IkeyBoardMouseClickService;
        InterfaceWriteDataViewModel InterfaceWriteDataViewModel;
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                SetProperty(ref _Name, value);
            }
        }

        private string _address;
        public string Address
        {
            get { return _address; }
            set
            {
                SetProperty(ref _address, value);
            }
        }
        private string _dbIndex;
        public string DbIndex
        {
            get => _dbIndex;
            set { SetProperty(ref _dbIndex, value); }
        }
        private string dbDataIndex;
        public string DbDataIndex
        {
            get => dbDataIndex;
            set { SetProperty(ref dbDataIndex, value); }
        }

        public ObservableCollection<string> _varTypeCollection = new ObservableCollection<string>()
        {
           "Bit","Word","DWord","Int","DInt","Real"

        };
        public ObservableCollection<string> VarTypeCollection
        {
            get => _varTypeCollection;
            set { SetProperty(ref _varTypeCollection, value); }
        }
        public int _varTypeIndex;
        public int VarTypeIndex
        {
            get => _varTypeIndex;
            set { SetProperty(ref _varTypeIndex, value); }
        }
        private ICommand _addItemCommand;
        public ICommand AddItemCommand
        {
            get
            {
                return this._addItemCommand ?? (this._addItemCommand = new DelegateCommand(() =>
                {
                    var values = Enum.GetValues(typeof(ReciveDataType));
                    VarType dataType = (VarType)values.GetValue(VarTypeIndex);
                    if (dataType == VarType.Bit)
                    {
                        WriteDataEvent.AddItem(new Model.InterfaceWriteBoolDataModel()
                        {
                            Name = this.Name,
                            Address = this.Address ??= $"DBD{this.DbIndex}.DB{values.GetValue(VarTypeIndex)}{this.dbDataIndex}",
                            DataType = (VarType)values.GetValue(VarTypeIndex),
                            DbIndex = int.Parse(this.DbIndex),
                            DbDataIndex = int.Parse(this.dbDataIndex)
                        });
                    }
                    else
                    {
                        WriteDataEvent.AddItem(new Model.InterfaceWriteDataModel()
                        {
                            Name = this.Name,
                            Address = this.Address ??= $"DBD{this.DbIndex}.DB{values.GetValue(VarTypeIndex)}{this.dbDataIndex}",
                            DataType = (VarType)values.GetValue(VarTypeIndex),
                            DbIndex = int.Parse(this.DbIndex),
                            DbDataIndex = int.Parse(this.dbDataIndex)
                        });
                    }
                   
                }));
            }
        }

        public AddWriteDataViewModel(InterfaceWriteDataViewModel interfaceWriteDataViewModel,
            WriteDataEventService writeDataEvent,
            IkeyBoardMouseClickService ikeyBoardMouseClickService)
        {
            WriteDataEvent = writeDataEvent;
            InterfaceWriteDataViewModel = interfaceWriteDataViewModel;
            IkeyBoardMouseClickService = ikeyBoardMouseClickService;
          //  inputTimer = new Timer(CheckInputState, null, 0, checkInterval);
        }

        public void OnLoaded(object sender, RoutedEventArgs e)
        {



            window = sender as Window;
            if (window != null)
            {
                HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
                source.AddHook(WndProc);
            }
        }

        private void CheckInputState(object? state)
        {
            while (IkeyBoardMouseClickService.DispatchQueue.Count > 0)
            {
                var 委托 = IkeyBoardMouseClickService.DispatchQueue.Dequeue();
                委托.Invoke();

            }

            //bool isControlPressed = IkeyBoardMouseClickService.KeyMoniterCollection[Enum_KeyBoardButton.Control];
            //bool isZPressed = IkeyBoardMouseClickService.KeyMoniterCollection[Enum_KeyBoardButton.Z];
            //if (isControlPressed && isZPressed)
            //{
            //    if (!_hasTriggered)
            //    {
            //        _hasTriggered = true;
            //        MessageBox.Show("撤销");
            //    }
            //}
            //else
            //{
            //    _hasTriggered = false;
            //}
        }

        public void OnUnloaded(object sender, RoutedEventArgs e)
        {
            //inputTimer?.Dispose();//停止释放定时器
            //inputTimer = null;
        }
        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case Win32Message.WM_KEYDOWN:
                    int keycode = wParam.ToInt32();
                    if (keycode == (int)Enum_KeyBoardButton.Control)
                    {
                        IkeyBoardMouseClickService.KeyBoardMouseEueue(() =>
                        {
                            IkeyBoardMouseClickService.KeyMoniterCollection[Enum_KeyBoardButton.Control] = true;
                        });
                    }
                    if (keycode == (int)Enum_KeyBoardButton.Z)
                    {
                        IkeyBoardMouseClickService.KeyBoardMouseEueue(() =>
                        {
                            IkeyBoardMouseClickService.KeyMoniterCollection[Enum_KeyBoardButton.Z] = true;
                        });
                    }
                    break;
                case Win32Message.WM_KEYUP:
                    int upKeyCode = wParam.ToInt32();
                    if (upKeyCode == (int)Enum_KeyBoardButton.Control)
                    {
                        IkeyBoardMouseClickService.KeyBoardMouseEueue(() =>
                        {
                            IkeyBoardMouseClickService.KeyMoniterCollection[Enum_KeyBoardButton.Control] = false;
                        });
                    }
                    if (upKeyCode == (int)Enum_KeyBoardButton.Z)
                    {
                        IkeyBoardMouseClickService.KeyBoardMouseEueue(() =>
                        {
                            IkeyBoardMouseClickService.KeyMoniterCollection[Enum_KeyBoardButton.Z] = false;
                        });
                    }
                    break;
                default:
                    break;
            }
            return IntPtr.Zero;
        }
    }
}
